<?php

namespace Home\Controller;

use Think\Controller;

class WorkOrderController extends CommnController
{

    public function __construct()
    {
        parent::__construct();
    }
    //发布工单时获取接受人
    public function getaccept_name()
    {
        $apart = I('apart');
        switch ($apart) {
            case 1://客服
                $admin = M('admin', '', 'KEFU')->where(['role_id' => ['in', '1,2'], 'status' => 1])->field('id,name')->order('id desc')->select();
                break;

            case 2://运营 吴登密  王翎  陈秋利
                $admin = M('admin', '', 'USER')->where(['id' => ['in', '58,63,53'], 'status' => 1])->field('id,name')->select();
                break;
            case 4://风控
                $admin = M('fk_admin', '', 'KEFU_FK')->where(['id' => ['in', '1,6,7,8,13,14'], 'status' => 1])->field('id,name')->select();
                break;
            case 5://后端
                $admin = M('admin', '', 'USER')->where(['id' => ['in', '7,22,28,33,43'], 'status' => 1])->field('id,name')->select();
                break;
            case 6://贷后  王智  刘元芬
                $admin = M('admin', '', 'USER')->where(['id' => ['in', '27,39'], 'status' => 1])->field('id,name')->select();
                break;
        }
        $this->ajaxreturn(['code' => 1001, 'data' => $admin], 'json');
    }

    //添加工单
    public function addworkorder()
    {
        if (empty($_POST)) {
            $apart = M('apart', '', 'KEFU')->where(['id' => ['neq', 3]])->order('id asc')->select();
            $this->apart = $apart;
            $this->admin = M('admin', '', 'KEFU')->where(['role_id' => ['in', '1,2'], 'status' => 1])->field('id,name')->order('id desc')->select();
            $this->display();
            die;
        }
        $title = I('title');
        $content = I('content');
        $img = I('img');//图片使用 字符隔开
        $accept_apart_id = I('accept_apart_id');
        $user_name = I('user_name');
        $phone = I('phone');
        if (!empty($phone)) {
            $user_vid = M('user', '', 'USER')->where(['mobile' => $phone, 'truename' => $user_name])->getfield('vid');
            if (empty($user_vid)) {
                $this->ajaxReturn(array('state' => '2', 'result' => '请添加正确的手机号和用户名'), 'json');
            }
        }
        $imgurl = '';
        if (!empty($img)) {
            $upload = new \Admin\Logic\UploadPicLogic();        //实例化 上传图片
            $picarray = explode('~', $img);
            $url = "kefu/workorder/";
            foreach ($picarray as $k1 => $v1) {
                $returnimg = $upload->LoadIgoodimg($v1, $url);
                if ($returnimg['status'] == '1') {
                    if ($k1 == 0) {
                        $imgurl = $returnimg['pic'];
                    } else {
                        $imgurl = $imgurl . ',' . $returnimg['pic'];
                    }
                } else {
                    $this->ajaxReturn(array('state' => '2', 'result' => '图片上传失败'), 'json');
                }
            }
        }
        $apart_id = 1;
        $publish_name = $_SESSION['name'];
        //随机选择 该部门任一客服解决
        $where = [];
        $accept_name = I('accept_name');
        M('work_order', '', 'KEFU')->startTrans();
        M('work_order_detail', '', 'KEFU')->startTrans();
        $arr = array('level' => I('level'), 'apart_id' => $apart_id, 'publish_name' => $publish_name, 'accept_apart_id' => $accept_apart_id, 'accept_name' => $accept_name, 'title' => $title, 'user_name' => $user_name, 'user_phone' => $phone, 'ctime' => time(), 'upd_time' => time());
        $add = M('work_order', '', 'KEFU')->add($arr);
        $arr = array('content' => $content, 'img' => $imgurl, 'wo_id' => $add);
        $add1 = M('work_order_detail', '', 'KEFU')->add($arr);
        if ($add && $add1) {

            if($accept_apart_id=='5'){//后端
                send_sms('15105876874', '大佬，你有新的工单，请注意查收！【共享现金】');//振业
                send_sms('13587989359', '大佬，你有新的工单，请注意查收！【共享现金】');//莎蓓
            }

            $str = array('aid' => $_SESSION['id'], 'content' => '创建工单' . $add, 'ctime' => time());
            M('admin_log', '', 'KEFU')->add($str);
            M('work_order', '', 'KEFU')->commit();
            M('work_order_detail', '', 'KEFU')->commit();
            $this->ajax('1001', '添加成功');
            die;
        } else {
            M('work_order', '', 'KEFU')->rollback();
            M('work_order_detail', '', 'KEFU')->rollback();
            $this->ajax('1002', '添加失败');
        }
    }

    /**
     * 关闭工单
     */
    public function close()
    {
        if (M('work_order', '', 'KEFU')->where(['id' => I('id')])->save(['status' => 4])) {
            $str = array('aid' => $_SESSION['id'], 'content' => '工单' . I('id') . '关闭', 'ctime' => time());
            M('admin_log', '', 'KEFU')->add($str);
            $this->ajaxreturn(['code' => 1001, 'result' => '关闭工单成功'], 'json');
        } else {
            $this->ajaxreturn(['code' => 1002, 'result' => '关闭工单失败'], 'json');
        }
    }

    /**
     * 补充说明
     */
    public function extra_content()
    {
        $id = I('id');
        $data['extra_content'] = I('extra_content');
        if (M('work_order_detail', '', 'KEFU')->where(['wo_id' => $id])->save($data)) {
            $str = array('aid' => $_SESSION['id'], 'content' => '工单' . $id . '修改补充说明', 'ctime' => time());
            M('admin_log', '', 'KEFU')->add($str);
            $this->ajaxreturn(['code' => 1001, 'result' => '工单补充说明编辑成功'], 'json');
        } else {
            $this->ajaxreturn(['code' => 1002, 'result' => '工单补充说明编辑失败'], 'json');
        }
    }

    /**
     * 工单详情
     */
    public function details1()
    {
        $this->rid = I('rid');
        $type = I('get.type');
        $this->type1 = $type;
        $this->page = I('page');
        $id = I('get.id');
        $info = M('work_order', '', 'KEFU')->join('b join work_order_detail a on b.id=a.wo_id')->where(['b.id' => $id])->field('a.extra_content,b.accept_name,b.apart_id,b.publish_name,b.accept_apart_id,b.status,b.ctime,a.content,a.img,b.upd_time,b.user_name,b.user_phone')->find();
        $status = $info['status'];
        $this->apart_id = $info['apart_id'];
        $this->name = $_SESSION['name'];
        $this->accept_apart_id = $info['accept_apart_id'];
        if ($info['apart_id'] == 1 && $info['publish_name'] == $_SESSION['name']) {
            $type = 3;
        } elseif ($info['accept_apart_id'] == 1 && $info['accept_name'] == $_SESSION['name']) {
            if ($info['status'] == 1 || $info['status'] == 2) {
                $type = 2;
            }
            if ($info['status'] == 3) {
                $type = 4;
            }
        } elseif ($info['accept_apart_id'] == 1 && $info['accept_name'] != $_SESSION['name'] && ($info['status'] == 1 || $info['status'] == 2)) {
            $type = 5;
        }

        $apart_f = M('apart', '', 'KEFU')->where(['id' => $info['apart_id']])->getField('name');
        $apart_s = M('apart', '', 'KEFU')->where(['id' => $info['accept_apart_id']])->getField('name');
        $info['apart_id'] = $apart_f;
        $info['accept_apart_id'] = $apart_s;
        $info['ctime'] = date('Y-m-d H:i', $info['ctime']);
        switch ($info['status']) {
            case 1:
                $info['status'] = '待处理';
                break;
            case 2:
                $info['status'] = '已回复';
                break;
            case 3:
                $info['status'] = '已完成';
                break;
            case 4:
                $info['status'] = '已取消';
                break;
            case 5:
                $info['status'] = '已删除';
                break;
        }
        if (!empty($info['img'])) {
            $images = explode(',', $info['img']);
        } else {
            $images = '';
        }
        $apart = M('apart', '', 'KEFU')->select();
//            //操作记录
        $record_feedback = M('work_order_answer', '', 'KEFU')->join('b join apart a on b.apart_id = a.id')->where(['w_id' => $id])->field('a.name as apart_name,b.name,b.content,b.ctime,b.img')->select();//回复记录
        foreach ($record_feedback as $k => $v) {
            $record_feedback[$k]['ctime'] = date('Y-m-d H:i', $v['ctime']);
            $record_feedback[$k]['type'] = '1';
            $record_feedback[$k]['img'] = explode(',', $v['img']);
        }
        $record_log = M('work_order_log', '', 'KEFU')->join('b join apart a on b.apart_id = a.id')->where(['work_id' => $id])
            ->field('a.name as apart_name,b.change_name as name,b.accept_apart_id as content,b.ctime')->select();//转交记录
        foreach ($record_log as $k => $v) {
            $record_log[$k]['ctime'] = date('Y-m-d H:i', $v['ctime']);
            $record_log[$k]['type'] = '2';
            $record_log[$k]['img'] = '';
        }
        $total = array_merge($record_feedback, $record_log);
        $time = array();
        foreach ($total as $k) {
            $time[] = $k['ctime'];
        }
        array_multisort($time, SORT_ASC, $total);
        if ($status == 3 || $status == 4) {
            $s_arr['apart_name'] = $_SESSION['name'];
            $s_arr['accept_apart_id'] = '客服部门';
            $s_arr['ctime'] = $info['upd_time'];
            $s_arr['type'] = $status;
            $s_arr['img'] = '';
            array_push($total, $s_arr);
        }
        $this->assign(array(
            'data' => $info,
            'type' => $type,
            'imgs1' => $images,
            'apart' => $apart,
            'id' => $id,
            'status' => $status,
            'work_log' => $total
        ));
        $this->display();

    }

    /**
     * 工单详情
     */
    public function details()
    {
        $this->rid = I('rid');
        $type = I('get.type');
        $this->type1 = $type;
        $this->page = I('page');
        $id = I('get.id');
        $info = M('work_order', '', 'KEFU')->join('b join work_order_detail a on b.id=a.wo_id')->where(['b.id' => $id])->field('a.extra_content,b.accept_name,b.apart_id,b.publish_name,b.accept_apart_id,b.status,b.ctime,a.content,a.img,b.upd_time,b.user_name,b.user_phone')->find();
        $status = $info['status'];
        $this->apart_id = $info['apart_id'];
        $this->name = $_SESSION['name'];
        $this->accept_apart_id = $info['accept_apart_id'];
        if ($info['apart_id'] == 1 && $info['publish_name'] == $_SESSION['name']) {
            $type = 3;
        } elseif ($info['accept_apart_id'] == 1 && $info['accept_name'] == $_SESSION['name']) {
            if ($info['status'] == 1 || $info['status'] == 2) {
                $type = 2;
            }
            if ($info['status'] == 3) {
                $type = 4;
            }
        } elseif ($info['accept_apart_id'] == 1 && $info['accept_name'] != $_SESSION['name'] && ($info['status'] == 1 || $info['status'] == 2)) {
            $type = 5;
        }

        $apart_f = M('apart', '', 'KEFU')->where(['id' => $info['apart_id']])->getField('name');
        $apart_s = M('apart', '', 'KEFU')->where(['id' => $info['accept_apart_id']])->getField('name');
        $info['apart_id'] = $apart_f;
        $info['accept_apart_id'] = $apart_s;
        $info['ctime'] = date('Y-m-d H:i', $info['ctime']);
        switch ($info['status']) {
            case 1:
                $info['status'] = '待处理';
                break;
            case 2:
                $info['status'] = '已回复';
                break;
            case 3:
                $info['status'] = '已完成';
                break;
            case 4:
                $info['status'] = '已取消';
                break;
            case 5:
                $info['status'] = '已删除';
                break;
        }
        if (!empty($info['img'])) {
            $images = explode(',', $info['img']);
        } else {
            $images = '';
        }
        $apart = M('apart', '', 'KEFU')->where(['id' => ['neq', 3]])->select();
        $this->admin = M('admin', '', 'KEFU')->where(['role_id' => ['in', '1,2'], 'status' => 1])->field('id,name')->order('id desc')->select();
//            //操作记录
        $record_feedback = M('work_order_answer', '', 'KEFU')->join('b join apart a on b.apart_id = a.id')->where(['w_id' => $id])->field('a.name as apart_name,b.name,b.content,b.ctime,b.img')->select();//回复记录
        foreach ($record_feedback as $k => $v) {
            $record_feedback[$k]['ctime'] = date('Y-m-d H:i', $v['ctime']);
            $record_feedback[$k]['type'] = '1';
            $record_feedback[$k]['img'] = explode(',', $v['img']);
        }
        $record_log = M('work_order_log', '', 'KEFU')->join('b join apart a on b.apart_id = a.id')->where(['work_id' => $id])
            ->field('a.name as apart_name,b.change_name as name,b.accept_apart_id as content,b.ctime')->select();//转交记录
        foreach ($record_log as $k => $v) {
            $record_log[$k]['ctime'] = date('Y-m-d H:i', $v['ctime']);
            $record_log[$k]['type'] = '2';
            $record_log[$k]['img'] = '';
        }
        $total = array_merge($record_feedback, $record_log);
        $time = array();
        foreach ($total as $k) {
            $time[] = $k['ctime'];
        }
        array_multisort($time, SORT_ASC, $total);
        if ($status == 3 || $status == 4) {
            $s_arr['apart_name'] = $_SESSION['name'];
            $s_arr['accept_apart_id'] = '客服部门';
            $s_arr['ctime'] = $info['upd_time'];
            $s_arr['type'] = $status;
            $s_arr['img'] = '';
            array_push($total, $s_arr);
        }
        $this->assign(array(
            'data' => $info,
            'type' => $type,
            'imgs1' => $images,
            'apart' => $apart,
            'id' => $id,
            'status' => $status,
            'work_log' => $total
        ));
        $this->display();

    }

    /**
     * 工单列表
     */
    public function workorderlist()
    {
        $this->roles = role(I('rid'));
        $this->rid = I('rid');
        $work_order = M('work_order', '', 'KEFU');
        $row = '10';
        $type = I('get.type', '1');
        $where = [];
        $where['status'] = array('in', array('1', '2'));
        $where['accept_apart_id'] = 1;
        $where['accept_name'] = array('eq', $_SESSION['name']);
        $num = $work_order->where($where)->count();
        $where = [];
        $search = I('get.content');
        $where['a.status'] = ['neq', 5];
        if (!empty($search)) {
            $where['d.content|a.title|a.publish_name'] = array('like', '%' . $search . '%');
        }

        $this->search = $search;
        switch ($type) {
            case 1:
                break;
            case 2:
                $where['a.status'] = ['in', '1,2'];
                $where['a.accept_apart_id'] = 1;
                $where['a.accept_name'] = $_SESSION['name'];
                break;
            case 3:
                $where['a.apart_id'] = 1;
                $where['a.publish_name'] = $_SESSION['name'];
                break;
            case 4:
                $where['a.status'] = 3;
                $where['a.apart_id'] = 1;
                $where['a.publish_name'] = $_SESSION['name'];
                break;
            case 5:
                $where['a.status'] = ['in', '1,2'];
                $where['a.accept_apart_id'] = 1;
                break;
        }

        $tol = $work_order->where($where)->alias('a')->join('apart b on a.apart_id=b.id')->join('apart c on a.accept_apart_id=c.id')->join('work_order_detail d on a.id=d.wo_id')->count('a.id');
        $page = new PageController($tol, $row);
        $fpage = $page->fpage();
        $field = 'a.id,a.publish_name,a.accept_name,a.title,a.ctime,a.upd_time,a.level,a.status,b.name as apart_f,c.name as apart_s,a.user_name,a.user_phone,d.content';
        $data = $work_order->where($where)->alias('a')->join('apart b on a.apart_id=b.id')->join('work_order_detail d on a.id=d.wo_id')->join('apart c on a.accept_apart_id=c.id')->field($field)->order('id desc')->LIMIT($page->listfirst, $page->listRows)->select();


        foreach ($data as $k => $v) {
            switch ($v['level']) {
                case 1:
                    $data[$k]['level'] = '一般';
                    break;
                case 2:
                    $data[$k]['level'] = '加急';
                    break;
                case 3:
                    $data[$k]['level'] = '紧急';
                    break;
            }
            //回复状态
            switch ($v['status']) {
                case 1:
                    $data[$k]['status'] = '待处理';
                    break;
                case 2:
                    $data[$k]['status'] = '已回复';
                    break;
                case 3:
                    $data[$k]['status'] = '完成';
                    break;
                case 4:
                    $data[$k]['status'] = '关闭';
                    break;
                case 5:
                    $data[$k]['status'] = '删除';
                    break;
            }
            if (!empty($v['ctime'])) {
                $data[$k]['ctime'] = date('Y-m-d H:i', $v['ctime']);
            }
            if (!empty($v['upd_time'])) {
                $data[$k]['upd_time'] = date('Y-m-d H:i', $v['upd_time']);
            }
        }
        $this->assign(array(
            'data' => $data,
            'fpage' => $fpage,
            'tol' => $tol,
            'type' => $type,
            'num' => $num
        ));
        $this->page = I('page');
        $this->display();

    }

    /**
     * 创建工单
     */
    public function addworkorder1()
    {
        if (empty($_POST)) {
            $apart = M('apart', '', 'KEFU')->select();
            $this->apart = $apart;
            $this->display();
            die;
        }
        $title = I('title');
        $content = I('content');
        $img = I('img');
        $accept_apart_id = I('accept_apart_id');
        $user_name = I('user_name');
        $phone = I('phone');
        if (!empty($phone)) {
            $user_vid = M('user', '', 'USER')->where(['mobile' => $phone, 'truename' => $user_name])->getfield('vid');
            if (empty($user_vid)) {
                $this->ajaxReturn(array('state' => '2', 'result' => '请添加正确的手机号和用户名'), 'json');
            }
        }


        $imgurl = '';
        if (!empty($img)) {
            $upload = new \Admin\Logic\UploadPicLogic();        //实例化 上传图片
            $picarray = explode('~', $img);
            $url = "kefu/workorder/";
            foreach ($picarray as $k1 => $v1) {
                $returnimg = $upload->LoadIgoodimg($v1, $url);
                if ($returnimg['status'] == '1') {
                    if ($k1 == 0) {
                        $imgurl = $returnimg['pic'];
                    } else {
                        $imgurl = $imgurl . ',' . $returnimg['pic'];
                    }
                } else {
                    $this->ajaxReturn(array('state' => '2', 'result' => '图片上传失败'), 'json');
                }
            }
        }
        $apart_id = 1;
        $publish_name = $_SESSION['name'];
        //随机选择 该部门任一客服解决
        $where = [];
        if ($accept_apart_id == 1) {
            $where['id'] = array('neq', $_SESSION['id']);
            $accept_name = M('admin', '', 'KEFU')->where($where)->order("rand()")->limit(1)->select();
        }
        if ($accept_apart_id == 4) {
            $accept_name = M('fk_admin', '', 'KEFU_FK')->where($where)->order("rand()")->limit(1)->select();
        }
        if ($accept_apart_id == 5) {
            $accept_name = M('admin', '', 'USER')->where($where)->order("rand()")->limit(1)->select();
        }
        M('work_order', '', 'KEFU')->startTrans();
        M('work_order_detail', '', 'KEFU')->startTrans();
        $arr = array('level' => I('level'), 'apart_id' => $apart_id, 'publish_name' => $publish_name, 'accept_apart_id' => $accept_apart_id, 'accept_name' => $accept_name[0]['name'], 'accept_id' => $accept_name[0]['id'], 'title' => $title, 'user_name' => $user_name, 'user_phone' => $phone, 'ctime' => time(), 'upd_time' => time());
        $add = M('work_order', '', 'KEFU')->add($arr);
        $arr = array('content' => $content, 'img' => $imgurl, 'wo_id' => $add);
        $add1 = M('work_order_detail', '', 'KEFU')->add($arr);
        if ($add && $add1) {
            $str = array('aid' => $_SESSION['id'], 'content' => '创建工单' . $add, 'ctime' => time());
            M('admin_log', '', 'KEFU')->add($str);
            M('work_order', '', 'KEFU')->commit();
            M('work_order_detail', '', 'KEFU')->commit();
            $this->ajax('1001', '添加成功');
            die;
        } else {
            M('work_order', '', 'KEFU')->rollback();
            M('work_order_detail', '', 'KEFU')->rollback();
            $this->ajax('1002', '添加失败');
        }
    }

    /**
     * 增加工单
     */
    public function workorder_addworkorder_add()
    {
        if (empty($_POST)) {
            $apart = M('apart', '', 'KEFU')->select();
            $this->assign(array(
                'apart' => $apart
            ));
            $this->display();
            die;
        }
        $title = I('title');
        $content = I('content');
        $img = I('img');
        $accept_apart_id = I('accept_apart_id');
        $user_name = I('user_name');
        $phone = I('phone');
        $apart_id = $_SESSION['role_id'];
        $publish_name = $_SESSION['name'];
        //随机选择 该部门任一客服解决
        $where = [];
        $where['role_id'] = array('eq', $apart_id);

        if ($accept_apart_id == 1) {
            $where['id'] = array('neq', $_SESSION['id']);
            $accept_name = M('admin', '', 'KEFU')->where($where)->order("rand()")->limit(1)->select();
        }
        if ($accept_apart_id == 4) {
            $accept_name = M('fk_admin', '', 'KEFU_FK')->where($where)->order("rand()")->limit(1)->select();
        }
        if ($accept_apart_id == 5) {
            $accept_name = M('admin', '', 'USER')->where($where)->order("rand()")->limit(1)->select();
        }

        $arr = array('apart_id' => $apart_id, 'publish_name' => $publish_name, 'accept_apart_id' => $accept_apart_id, 'accept_name' => $accept_name[0]['name'], 'accept_id' => $accept_name[0]['id'], 'title' => $title, 'user_name' => $user_name, 'user_phone' => $phone, 'ctime' => time(), 'upd_time' => time());

        $add = M('work_order', '', 'KEFU')->add($arr);

        $arr = array('content' => $content, 'img' => $img, 'wo_id' => $add);

        $add1 = M('work_order_detail', '', 'KEFU')->add($arr);

        if ($add && $add1) {

            $str = array('aid' => $_SESSION['id'], 'content' => '创建工单' . $add, 'ctime' => time());
            M('admin_log', '', 'KEFU')->add($str);

            $this->ajax('1001', '添加成功');
            die;
        }
        $this->ajax('1002', '添加失败');


    }

    /**
     * 工单列表
     */
    public function workorder_list()
    {
        $work_order = M('work_order', '', 'KEFU');

        $row = '12';

        $type = I('get.type', '1');

        $where = [];
        $where['status'] = array('in', array('1', '2'));
        $where['accept_apart_id'] = array('eq', $_SESSION['role_id']);
        $where['accept_name'] = array('eq', $_SESSION['name']);
//          echo $_SESSION['name'].'=='.$_SESSION['role_id'];die;
        $num = $work_order->where($where)->count();

        $where = [];
        if ($type == 1) {//全部工单
            $tol = $work_order->count();
            $page = new PageController($tol, $row);
            $fpage = $page->fpage();
            $data = $work_order->order('id desc')->LIMIT($page->listfirst, $page->listRows)->select();

        } else if ($type == 2) {//受理中工单

            $where['status'] = array('in', array('1', '2'));
            $where['accept_apart_id'] = array('eq', $_SESSION['role_id']);
            $where['accept_name'] = array('eq', $_SESSION['name']);
            $tol = $work_order->where($where)->count();
            $page = new PageController($tol, $row);
            $fpage = $page->fpage();
            $data = $work_order->where($where)->order('id desc')->LIMIT($page->listfirst, $page->listRows)->select();
        } else if ($type == 3) {//我提交工单
            $where['apart_id'] = array('eq', $_SESSION['role_id']);
            $where['publish_name'] = array('eq', $_SESSION['name']);
            $tol = $work_order->where($where)->count();
            $page = new PageController($tol, $row);
            $fpage = $page->fpage();
            $data = $work_order->where($where)->order('id desc')->LIMIT($page->listfirst, $page->listRows)->select();
        } else if ($type == 4) {//我完成工单

            $where['status'] = array('eq', '3');
            $where['apart_id'] = array('eq', $_SESSION['role_id']);
            $where['publish_name'] = array('eq', $_SESSION['name']);
            $tol = $work_order->where($where)->count();
            $page = new PageController($tol, $row);
            $fpage = $page->fpage();
            $data = $work_order->where($where)->order('id desc')->LIMIT($page->listfirst, $page->listRows)->select();
        } else if ($type == 5) {//组内未处理工单
            $where['status'] = array('in', array('1', '2'));
            $where['accept_apart_id'] = array('eq', $_SESSION['role_id']);
            $tol = $work_order->where($where)->count();
            $page = new PageController($tol, $row);
            $fpage = $page->fpage();
            $data = $work_order->where($where)->order('id desc')->LIMIT($page->listfirst, $page->listRows)->select();
        }

//        $tol = count($data);
        foreach ($data as $k => $v) {

            $apart_f = M('apart', '', 'KEFU')->where(['id' => $v['apart_id']])->getField('name');
            $apart_s = M('apart', '', 'KEFU')->where(['id' => $v['accept_apart_id']])->getField('name');

            $data[$k]['apart_f'] = $apart_f;
            $data[$k]['apart_s'] = $apart_s;

            //回复状态
            if ($v['status'] == '1') {
                $data[$k]['status'] = '待处理';
            } else if ($v['status'] == '2') {
                $data[$k]['status'] = '已回复';
            } else if (($v['status'] == '3')) {
                $data[$k]['status'] = '完成';
            } else if (($v['status'] == '4')) {
                $data[$k]['status'] = '已删除';
            }
            $data[$k]['ctime'] = date('Y-m-d H:i', $v['ctime']);
            $data[$k]['upd_time'] = date('Y-m-d H:i', $v['upd_time']);

        }

        $this->assign(array(
            'data' => $data,
            'fpage' => $fpage,
            'tol' => $tol,
            'type' => $type,
            'num' => $num
        ));
        $this->display();


    }


    /**
     * 工单详情 回复
     */
    public function workorder_feedback()
    {

        $id = I('id');
        $content = I('back_content');
        $img = I('img');
        $arr = array('w_id' => $id, 'apart_id' => 1, 'name' => $_SESSION['name'], 'content' => $content, 'img' => $img, 'ctime' => time());
        $wo = M('work_order', '', 'KEFU')->where(['id' => $id])->field('apart_id,publish_name,status')->find();
        M('work_order_answer', '', 'KEFU')->startTrans();
        M('work_order', '', 'KEFU')->startTrans();
        $info = M('work_order_answer', '', 'KEFU')->add($arr);
        if ($info) {
            if ($wo['publish_name'] != $_SESSION['name'] && $wo['status'] == 1) {
                $data['status'] = 2;
            }
            $data['upd_time'] = time();
            if (!M('work_order', '', 'KEFU')->where(['id' => $id])->save($data)) {//工单更新时间
                M('work_order_answer', '', 'KEFU')->rollback();
                M('work_order', '', 'KEFU')->rollback();
            }
            $str = array('aid' => $_SESSION['id'], 'content' => '回复工单' . $id . '记录为' . $info, 'ctime' => time());
            M('admin_log', '', 'KEFU')->add($str);
            M('work_order_answer', '', 'KEFU')->commit();
            M('work_order', '', 'KEFU')->commit();
            $this->ajaxReturn(['code' => '1001', 'result' => '回复成功']);
            die;
        }
        M('work_order_answer', '', 'KEFU')->rollback();
        M('work_order', '', 'KEFU')->rollback();
        $this->ajaxReturn(['code' => '1002', 'result' => '回复失败']);
    }

    /**
     * 转交工单
     */
    public function workorder_turn()
    {
        if (!IS_POST) {
            $this->ajaxReturn(['status' => '3', 'result' => '操作异常'], 'json');
        }
        M('work_order_log', '', 'KEFU')->startTrans();
        M('work_order', '', 'KEFU')->startTrans();
        $arr = array();
        $data = I('post.');
        $order = M('work_order', '', 'KEFU')->where(['id' => I('work_id')])->field('title,apart_id,publish_name,accept_apart_id,accept_name')->find();
        $adminwhere['status'] = 1;
        $data['accept_name'] = I('accept_name');
        $data['apart_id'] = 1;
        $data['change_name'] = $_SESSION['name'];
        $data['ctime'] = time();
        if (M('work_order_log', '', 'KEFU')->add($data)) {
            $orderdata['upd_time'] = time();
            $orderdata['accept_apart_id'] = I('apart');
            $orderdata['accept_name'] = $data['accept_name'];
            if (M('work_order', '', 'KEFU')->where(['id' => I('work_id')])->save($orderdata)) {
                M('work_order_log', '', 'KEFU')->commit();
                M('work_order', '', 'KEFU')->commit();
                $str = array('aid' => $_SESSION['id'], 'content' => '转交工单' . I('work_id'), 'ctime' => time());
                M('admin_log', '', 'KEFU')->add($str);
                $this->ajaxReturn(['code' => '1001', 'result' => '转交成功']);
            } else {
                M('work_order_log', '', 'KEFU')->rollback();
                M('work_order', '', 'KEFU')->rollback();
                $this->ajaxReturn(['code' => '1002', 'result' => '转交失败']);
            }
        } else {
            M('work_order_log', '', 'KEFU')->rollback();
            M('work_order', '', 'KEFU')->rollback();
            $this->ajaxReturn(['code' => '1002', 'result' => '转交失败', 'err' => $data]);
        }
    }

    /**
     * 完结工单
     */
    public function workorder_finish()
    {
        $id = I('id');
        $info = M('work_order', '', 'KEFU')->where(['id' => $id])->setfield('status', '3');
        if ($info) {
            $str = array('aid' => $_SESSION['id'], 'content' => '完结工单' . $id, 'ctime' => time());
            M('admin_log', '', 'KEFU')->add($str);
            $update = M('work_order', '', 'KEFU')->where(['id' => $id])->setfield('upd_time', time());//工单更新时间
            $this->ajaxReturn(['code' => '1001', 'result' => '完成']);
            die;
        }
        $this->ajaxReturn(['code' => '1002', 'result' => '失败']);

    }

    public function ajax($code, $result, $r = '')
    {
        if (empty($r)) {
            $this->ajaxReturn(['code' => $code, 'result' => $result]);
        } else {
            $this->ajaxReturn(['code' => $code, 'result' => $result]);
            return false;
        }
    }

}